package com.atguigu.controller;

import com.atguigu.entity.House;
import com.atguigu.entity.HouseImage;
import com.atguigu.result.Result;
import com.atguigu.service.HouseImageService;
import com.atguigu.service.HouseService;
import com.atguigu.util.QiniuUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/houseImage")
public class HouseImageController {
    @DubboReference
    private HouseImageService houseImageService;
    @DubboReference
    private HouseService houseService;

    /*渲染文件上传视图*/
    @RequestMapping("/uploadShow/{houseId}/{type}")
    public String uploadShow(
            @PathVariable("houseId") Long houseId,
            @PathVariable("type") Integer type,
            ModelMap map
    ){
        map.addAttribute("houseId",houseId);
        map.addAttribute("type",type);
        return "house/upload";
    }

    /*接收文件并保存文件的处理器  */
    @RequestMapping("/upload/{houseId}/{type}")
    @ResponseBody
    public Result upload(
            @PathVariable("houseId") Long houseId,
            @PathVariable("type") Integer type,
            @RequestParam("file") MultipartFile [] multipartFiles
            ) throws IOException {
        // 迭代 multipartFiles
        for (int i = 0; i < multipartFiles.length; i++) {
            // 存储图片到七牛云
            MultipartFile multipartFile=multipartFiles[i];
            // 生成文件名
            String filename = UUID.randomUUID().toString();
            // 存入七牛云
            QiniuUtil.upload2Qiniu(multipartFile.getBytes(),filename);

            // 创建图片的url
            String imageUrl ="http://rkfuqvxpq.hn-bkt.clouddn.com/"+filename;

            // 存储HouseImage到数据库
            HouseImage houseImage =new HouseImage();
            houseImage.setHouseId(houseId);
            houseImage.setType(type);
            houseImage.setImageName(filename);
            houseImage.setImageUrl(imageUrl);
            houseImageService.insert(houseImage);

            // 图片上传后,更新house表格中的defaultImageURl
            // 如果更新的是房源图片,那么就将第一张图片作为房子的默认图片
            if(type ==1 && i ==0){
                House house =new House();
                house.setId(houseId);
                house.setDefaultImageUrl(imageUrl);
                houseService.update(house);
            }

        }


        return Result.ok();
    }

    /*delete/[[${house.id}]]/'+id
    * 删除图片
    * */

    @RequestMapping("/delete/{houseId}/{id}")
    public String delete(
            @PathVariable("houseId") Long houseId,
            @PathVariable("id") Long id

    ){
        HouseImage houseImage = houseImageService.getById(id);
        // 调用服务层方法,删除图片
        houseImageService.delete(id);
        // 七牛云中对应的图片删除
        //QiniuUtil.deleteFileFromQiniu(houseImage.getImageName());

        return "redirect:/house/"+houseId;
    }
}
